package com.lpf.MyGameSaveBackup.GameSaveBackup.controller;
import com.alibaba.fastjson.JSONObject;
import com.lpf.MyGameSaveBackup.GameSaveBackup.vo.GameSaveBackVo;
import com.lpf.MyGameSaveBackup.utils.AlertUtil;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;
import lombok.Data;

import java.io.File;
@Data
public class GameDialogController {
    @FXML
    private TextField gameNameField;
    @FXML
    private TextField gameEnNameField;
    @FXML
    private TextField gameSavePathField;
    @FXML
    private TextField backupPathField;

    @FXML
    private Button savePathBrowseButton;
    @FXML
    private Button backupPathButton;
    @FXML
    private Button okButton;
    @FXML
    private Button cancelButton;

    private Stage dialogStage;
    private GameSaveBackVo game;
    private boolean okClicked = false;

    @FXML
    private void initialize() {
    }


    public void setGame(GameSaveBackVo game) {
        this.game = game;
        gameNameField.setText(game.getGameName());
        gameEnNameField.setText(game.getGameEnName());
        gameSavePathField.setText(game.getGameSavePath());
        backupPathField.setText(game.getBackupPath());
    }

    public GameSaveBackVo getGame() {
        GameSaveBackVo item = new GameSaveBackVo();
        item.setGameName(gameNameField.getText());
        item.setGameEnName(gameEnNameField.getText());
        item.setGameSavePath(gameSavePathField.getText());
        item.setBackupPath(backupPathField.getText());
        return item;
    }



    @FXML
    private void handleOk() {
        if (isInputValid()) {
            okClicked = true;
            dialogStage.close();
        }
    }

    @FXML
    private void handleCancel() {
        dialogStage.close();
    }

    //文件目录选取
    @FXML
    private void handleSelectPath(ActionEvent event) {
        // 获取触发事件的按钮
        Button clickButton = (Button) event.getSource();

        // 解析 userData 中的参数
        String userData = (String) clickButton.getUserData();
        JSONObject param = JSONObject.parseObject(userData);
        String targetFieldId = param.getString("targetFieldId");
        String dialogTitle = param.getString("title");
        // 根据 ID 获取目标 TextField（需确保 FXML 中 fx:id 与 ID 一致）
        TextField targetField = (TextField) clickButton.getScene().lookup("#" + targetFieldId);

        DirectoryChooser directoryChooser = new DirectoryChooser();
        directoryChooser.setTitle(dialogTitle);
        File selectedDir = directoryChooser.showDialog(dialogStage);
        if (selectedDir != null) {
            targetField.setText(selectedDir.getAbsolutePath());
        }
    }

    private boolean isInputValid() {
        String errorMessage = "";
        if (gameNameField.getText() == null || gameNameField.getText().isEmpty()) {
            errorMessage += "请输入游戏名称!\n";
        }

        if (gameSavePathField.getText() == null || gameSavePathField.getText().isEmpty()) {
            errorMessage += "请选择存档路径!\n";
        } else {
            File file = new File(gameSavePathField.getText());
            if (!file.exists() || !file.isDirectory()) {
                errorMessage += "存档路径不存在或不是一个文件夹!\n";
            }
        }
        if (backupPathField.getText() == null || backupPathField.getText().isEmpty()) {
            errorMessage += "请输入云端备份路径!\n";
        }

        if (errorMessage.isEmpty()) {
            return true;
        } else {
            // 显示错误信息
            AlertUtil.showErrorAlert("输入错误", "请修正以下错误", errorMessage);
            return false;
        }
    }
}